﻿@*
//------------------------------------------------------------------------------
//  此代码版权声明为全文件覆盖，如有原作者特别声明，会在下方手动补充
//  此代码版权（除特别声明外的代码）归作者本人Diego所有
//  源代码使用协议遵循本仓库的开源协议及附加协议
//  Gitee源代码仓库：https://gitee.com/diego2098/ThingsGateway
//  Github源代码仓库：https://github.com/kimdiego2098/ThingsGateway
//  使用文档：https://diego2098.gitee.io/thingsgateway-docs/
//  QQ群：605534569
//------------------------------------------------------------------------------
*@

@page "/index"
@layout MainLayout
@inherits CultureComponentBase
@attribute [Authorize]
@using Masa.Blazor.Presets
@namespace ThingsGateway.Web.Page
@inject UserResoures UserResoures;
<MCard Flat Class="ma-2">
    <MRow NoGutters Align="AlignTypes.Center" Justify="JustifyTypes.SpaceBetween">
        <div class="d-flex align-center justify-center ma-2">
            <MAvatar Size=40 Color="primary" Class="mx-2 white--text">
                @UserResoures.CurrentUser?.Account.GetNameLen2()
            </MAvatar>
            <MLabel>@(T("欢迎回来"))</MLabel>
                <MLabel Class="mx-3">@UserResoures.CurrentUser?.Account</MLabel>
            </div>
        </MRow>
    </MCard>

    <MCard MinHeight=120 Elevation="1" Class="ma-2">
        <MCardSubtitle Class="text-subtitle-1">
            @T("快捷方式")
        </MCardSubtitle>
        <MDivider></MDivider>
        <MRow Class="pa-4" NoGutters Align="AlignTypes.Center" Justify="JustifyTypes.Start">
        @foreach (var item in UserResoures.WorkbenchOutputs ?? new())
        {
            <MButton Text Href=@item.Component
                     Target=@(item.TargetType==TargetTypeEnum.SELF?"_self":"_blank") Class="ma-2 text-subtitle px-2">
                <MIcon Left Size="25" Color="primary">@item.Icon</MIcon>
                @item.Title
            </MButton>
        }
    </MRow>

</MCard>
@if (UserManager.SuperAdmin)
{
    <MRow NoGutters>
    <MCol Md=12 Cols="12">
        <MCard MinHeight=400 Elevation="1" Class="ma-2">
            <MCardSubtitle Class="text-subtitle-1">
                @T("RPC日志")
            </MCardSubtitle>
            <MDivider></MDivider>
            <MTimeline AlignTop Dense>
                @foreach (var item in RpcLogs ?? new())
                    {
                        <MTimelineItem Color="primary"
                                          Small>
                        <MRow Class="pt-1">
                            <MCol Cols="4">
                                <span class="text-caption">@item.LogTime.Add(JsInitVariables.TimezoneOffset).ToString("yyyy-MM-dd HH:mm:ss fff")</span>
                            </MCol>
                            <MCol Cols="8">
                                <div class="mt-1 px-2 d-flex align-center justify-space-between">
                                    <span>@item.OperateObject.Substring(0,item.OperateObject.Length>100?100:item.OperateObject.Length)</span>
                                    <span class="text-caption">@item.Description(a=>a.OperateObject)</span>
                                </div>
                                <div class="mt-1 px-2 d-flex align-center justify-space-between">
                                    <span>@item.OperateSource.Substring(0,item.OperateSource.Length>100?100:item.OperateSource.Length)</span>
                                    <span class="text-caption">@item.Description(a=>a.OperateSource)</span>
                                </div>
                                <div class="mt-1 px-2 d-flex align-center justify-space-between">
                                    <span>@item.ParamJson.Substring(0,item.ParamJson.Length>100?100:item.ParamJson.Length)</span>
                                    <span class="text-caption">@item.Description(a=>a.ParamJson)</span>
                                </div>
                                <div class="mt-1 px-2 d-flex align-center justify-space-between">
                                    <span>@item.ResultJson.Substring(0,item.ResultJson.Length>100?100:item.ResultJson.Length)</span>
                                    <span class="text-caption">@item.Description(a=>a.ResultJson)</span>
                                </div>

                            </MCol>
                        </MRow>
                    </MTimelineItem>

                    }

                </MTimeline>
            </MCard>
        </MCol>
        <MCol Md=12 Cols="12">
            <MCard MinHeight=400 Elevation="1" Class="ma-2">
                <MCardSubtitle Class="text-subtitle-1">
                    @T("后台日志")
                </MCardSubtitle>
                <MDivider></MDivider>
                <MTimeline AlignTop Dense>
                @foreach (var item in RuntimeLogs ?? new())
                    {
                        <MTimelineItem Color="primary"
                                          Small>
                        <MRow Class="pt-1">
                            <MCol Cols="4">
                                <span class="text-caption">@item.LogTime.Add(JsInitVariables.TimezoneOffset).ToString("yyyy-MM-dd HH:mm:ss fff")</span>
                            </MCol>
                            <MCol>
                                <div class="text-body-1">@item.LogSource.Substring(0,item.LogSource.Length>100?100:item.LogSource.Length)</div>
                                <div class="text-body-1">@item.LogMessage.Substring(0,item.LogMessage.Length>100?100:item.LogMessage.Length)</div>
                            </MCol>
                        </MRow>
                    </MTimelineItem>

                    }

                </MTimeline>
            </MCard>
        </MCol>
    </MRow>
}

<MRow NoGutters>
    <MCol Md=6 Cols="12">
        <MCard MinHeight=400 Elevation="1" Class="ma-2">
            <MCardSubtitle Class="text-subtitle-1">
                @T("访问日志")
            </MCardSubtitle>
            <MDivider></MDivider>
            <MTimeline AlignTop Dense>
                @foreach (var item in DevLogVisits ?? new())
                {
                    <MTimelineItem Color="primary"
                                      Small>
                    <MRow Class="pt-1">
                        <MCol Cols="4">
                            <span class="text-caption">@item.OpTime.Add(JsInitVariables.TimezoneOffset).ToString("yyyy-MM-dd HH:mm:ss fff")</span>
                        </MCol>
                        <MCol>
                            <div class="text-body-1">@item.Name</div>
                            <div class="text-caption">@item.OpIp</div>
                        </MCol>
                    </MRow>
                </MTimelineItem>

                }

            </MTimeline>
        </MCard>
    </MCol>
    <MCol Md=6 Cols="12">
        <MCard MinHeight=400 Elevation="1" Class="ma-2">
            <MCardSubtitle Class="text-subtitle-1">
                @T("操作日志")
            </MCardSubtitle>
            <MDivider></MDivider>
            <MTimeline AlignTop Dense>
                @foreach (var item in DevLogOps ?? new())
                {
                    <MTimelineItem Color="primary"
                                      Small>
                    <MRow Class="pt-1">
                        <MCol Cols="4">
                            <span class="text-caption">@item.OpTime.Add(JsInitVariables.TimezoneOffset).ToString("yyyy-MM-dd HH:mm:ss fff")</span>
                        </MCol>
                        <MCol>
                            <div class="text-body-1">@item.Name</div>
                            <div class="text-caption">@item.OpIp</div>
                        </MCol>
                    </MRow>
                </MTimelineItem>

                }

            </MTimeline>
        </MCard>
    </MCol>
</MRow>

@code {
    [Inject] public JsInitVariables JsInitVariables { get; set; } = default!;
    [Inject]
    private ThingsGateway.Web.Foundation.IRpcLogService RpcLogService { get; set; }
    [Inject]
    private ThingsGateway.Web.Foundation.IRuntimeLogService RuntimeLogService { get; set; }
    [Inject]
    private IVisitLogService VisitLogService { get; set; }
    List<DevLogVisit> DevLogVisits;
    List<DevLogOperate> DevLogOps;
    List<ThingsGateway.Web.Foundation.RpcLog> RpcLogs;
    List<ThingsGateway.Web.Foundation.RuntimeLog> RuntimeLogs;
    [Inject]
    private IOperateLogService OperateLogService { get; set; }
    protected override async Task OnInitializedAsync()
    {
        if (UserManager.SuperAdmin)
        {
            RpcLogs = (await RpcLogService.PageAsync(new() { Size = 5 })).Records.ToList();
            RuntimeLogs = (await RuntimeLogService.PageAsync(new() { Size = 5 })).Records.ToList();
        }

        DevLogVisits = (await VisitLogService.Page(new() { Size = 5, Account = UserResoures.CurrentUser?.Account })).Records.ToList();
        DevLogOps = (await OperateLogService.Page(new() { Size = 5, Account = UserResoures.CurrentUser?.Account })).Records.ToList();
        await base.OnInitializedAsync();
    }

}
